import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import qiankun from 'vite-plugin-qiankun'
import Unocss from 'unocss/vite'

// 获取环境变量中的应用名称
const packageName = require('./package.json').name

// https://vitejs.dev/config/
export default defineConfig({
  base: process.env.NODE_ENV === 'production' && process.env.VITE_PUBLIC_PATH ? process.env.VITE_PUBLIC_PATH : '/',
  plugins: [
    vue(),
    qiankun('vue-devops', {
      useDevMode: true
    }),
    Unocss(),
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  server: {
    port: 9005,
    cors: true,
    origin: 'http://localhost:9005',
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    proxy: {
      '/api/tracking': {
        target: 'http://localhost:3001/tracking',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/tracking/, '')
      }
    }
  },
  build: {
    // 确保不会按需引入 Element Plus
    commonjsOptions: {
      esmExternals: true 
    },
    rollupOptions: {
      // 不再外部化Vue，确保Vue被打包进应用
      output: {
        // 配置打包文件名的格式，便于主应用加载
        entryFileNames: 'js/[name].js',
        chunkFileNames: 'js/[name].[hash].js',
        assetFileNames: 'assets/[name].[hash].[ext]'
      }
    },
    // 确保构建后的文件是ES Module格式，以便主应用使用
    target: 'esnext',
    minify: 'terser'
  }
}) 